using System;
using System.Collections;
using System.Text;
using System.Web.UI.WebControls;
using PHSRAG.Insight21.CDS;
using PHSRAG.Insight21.Common.Pages;
using PHSRAG.Insight21.Policy;
using PHSRAG.Utility;
using Workflow.NET.Web.Designer;

namespace PHSRAG.Insight21.Humans
{
    /// <summary>
    /// This page is used from Humans admin section. this page holds Workflow process designer control
    /// and used to design the process.
    /// </summary>
    public partial class WorkFlowManager : BasePage
    {
        #region Protected Variables

        //WorkFlow Process designer
        protected ProcessDesigner WorkFlowMain;

        #endregion

        #region constants

        private const string PageCacheKey = "WorkFlowManager_903F42E8-471F-4791-9BD7-50EDD3841D7E";

        #endregion

        #region private Variables

        /// <summary>Application Cache Policy </summary>
        private ICachePolicy applicationCachePolicy;

        /// <summary> Policy used for caching Contexts to be used across pages </summary>
        private ICachePolicy pageCachePolicy;

        #endregion

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
            pageCachePolicy = (Policy.ICachePolicy) Session[Policy.Names.PageCachePolicyKey];
            applicationCachePolicy = (Policy.ICachePolicy) Application[Policy.Names.ApplicationCachePolicyKey];
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.header.Navigate += new System.Web.UI.WebControls.CommandEventHandler(this.OnNavigate);
            this.breadCrumb.Navigate += new System.Web.UI.WebControls.CommandEventHandler(this.OnNavigate);
            this.Load += new System.EventHandler(this.OnPageLoad);
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// This event just takes care of breadcrumb. All other activity is done on Workflow Proecess designer.
        /// </summary>
        /// <param name="sender">Sender of the event</param>
        /// <param name="e">Argument associated with the event</param>
        protected void OnPageLoad(object sender, EventArgs e)
        {
            // Put user code to initialize the page here

            breadCrumb.Add(
                " Workflow Management",
                "WorkFlowManager.aspx",
                Settings.GetKeyValue("BreadCrumb.Admin.Humans.WorkFlowManager", 1));
            RefCodes refCodes = (RefCodes) applicationCachePolicy.GetCachedObject(RefCodes.CacheKey);
            ArrayList humansWorkflow = refCodes.GetSortedRefCodes((adminSubNavigation.SelectedTab.ToLower() == "humans management") ?
                                                                                                                                        Constants.WorkFlowDomain.Humans : Constants.WorkFlowDomain.Animals, "Order");
            if (humansWorkflow != null & !IsPostBack)
            {
                if (humansWorkflow.Count > 0)
                {
                    foreach (RefCode refCode in humansWorkflow)
                    {
                        wFSelector.Items.Add(new ListItem(refCode.Meaning, refCode.Code));
                    }
                    wFSelector.DataTextField = "RefMeaning";
                    wFSelector.DataValueField = "RefCode";
                }
            }

            WorkFlowMain = new ProcessDesigner();
            WorkFlowMain.ApplicationName = Settings.GetKeyValue("InsightApplicationName", string.Empty);
            WorkFlowMain.WorkflowName = (adminSubNavigation.SelectedTab.ToLower() == "humans management") ?
                                                                                                              Constants.MainWorkFlow.Humans : Constants.MainWorkFlow.Animals;
            WorkFlowMain.Height = 800;
            ShowMainWorkFlow.Controls.Add(WorkFlowMain);
            adminHumansSubNavigation.Visible = (adminSubNavigation.SelectedTab.ToLower() == "humans management") ? true : false;
            adminAnimalsSubNavigation.Visible = !adminHumansSubNavigation.Visible;
            if (adminAnimalsSubNavigation.Visible)
            {
                adminAnimalsSubNavigation.SelectedLink = "Workflow Management";
            }
            else
            {
                adminHumansSubNavigation.SelectedLink = "Workflow Management";
            }
        }

        #endregion

        /// <summary>
        /// Opens the selected sub workflow as popup
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void OnViewWorkFlow(object sender, EventArgs e)
        {
            CacheOutGoingData();
            StringBuilder script = new StringBuilder();
            script.Append("\n<script language=JavaScript id='viewWF'>\n");
            script.Append("var editorWin = null; \n");
            script.Append("editorWin = window.open('DetailedWorkFlow.aspx', 'DetailedWorkFlow', 'width=1000, height=800, status=yes, resizable=yes toolbar=no'); \n");
            script.Append("</script>");
            if (!ClientScript.IsClientScriptBlockRegistered("DetailedWorkFlow"))
            {
                ClientScript.RegisterClientScriptBlock(GetType(), "DetailedWorkFlow", script.ToString());
            }
        }

        /// <summary>
        /// Caches outgoing data with incoming data key for use across pages.
        /// </summary>
        private void CacheOutGoingData()
        {
            Hashtable outgoingData = new Hashtable();
            outgoingData.Add(Keys.SelectedWFlowKey, wFSelector.SelectedValue);
            pageCachePolicy.Cache(Keys.IncomingDataKey, outgoingData);
        }

        /// <summary>
        /// Resets the subnavigation selected link before the header or the breadCrumb performs a redirect.
        /// </summary>
        /// <param name="sender">Control which rasied the event.</param>
        /// <param name="e">Arguments associated with the event.</param>
        private void OnNavigate(object sender, CommandEventArgs e)
        {
            adminSubNavigation.Reset();
            adminHumansSubNavigation.Reset();
        }
    }
}